<!DOCTYPE html>
<!-- Ported from the OpenGL Samples Pack https://github.com/g-truc/ogl-samples/blob/master/tests/gl-320-texture-offset.cpp -->
<html lang="en">

<head>
    <title>WebGL 2 Samples - texture_offset</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <div id="info">WebGL 2 Samples - texture_offset</div>
    <p id="description">
        This sample demonstrates the use of textureOffset(). <br>
        LEFT: With offset / RIGHT: Without offset
    </p>
    <!-- WebGL 2 shaders -->
    <script id="vs" type="x-shader/x-vertex">
        #version 300 es
        #define POSITION_LOCATION 0
        #define TEXCOORD_LOCATION 4
        
        precision highp float;
        precision highp int;

        uniform mat4 MVP;

        layout(location = POSITION_LOCATION) in vec2 position;
        layout(location = TEXCOORD_LOCATION) in vec2 texcoord;

        out vec2 v_st;

        void main()
        {
            v_st = texcoord;
            gl_Position = MVP * vec4(position, 0.0, 1.0);
        }
    </script>

    <script id="fs-offset-bicubic" type="x-shader/x-fragment">
        #version 300 es
        precision highp float;
        precision highp int;

        uniform sampler2D diffuse;
        uniform ivec2 offset;

        in vec2 v_st;

        out vec4 color;

        vec4 catmullRom(in vec4 A, in vec4 B, in vec4 C, in vec4 D, in float s)
        {
            mat4 catmullRomMatrix = mat4(
                vec4(-1.0, 2.0,-1.0, 0.0),
                vec4( 3.0,-5.0, 0.0, 2.0),
                vec4(-3.0, 4.0, 1.0, 0.0),
                vec4( 1.0,-1.0, 0.0, 0.0));

            vec4 expo = vec4(s * s * s, s * s, s, 1.0);

            return 0.5 * expo * catmullRomMatrix * mat4(
                A[0], B[0], C[0], D[0],
                A[1], B[1], C[1], D[1],
                A[2], B[2], C[2], D[2],
                A[3], B[3], C[3], D[3]);
        }

        vec4 textureCatmullrom(in sampler2D sampler, in vec2 texcoord, in vec2 offset)
        {
            vec4 texel00 = textureOffset(sampler, texcoord + offset, ivec2(-1,-1));
            vec4 texel10 = textureOffset(sampler, texcoord + offset, ivec2( 0,-1));
            vec4 texel20 = textureOffset(sampler, texcoord + offset, ivec2( 1,-1));
            vec4 texel30 = textureOffset(sampler, texcoord + offset, ivec2( 2,-1));

            vec4 texel01 = textureOffset(sampler, texcoord + offset, ivec2(-1, 0));
            vec4 texel11 = textureOffset(sampler, texcoord + offset, ivec2( 0, 0));
            vec4 texel21 = textureOffset(sampler, texcoord + offset, ivec2( 1, 0));
            vec4 texel31 = textureOffset(sampler, texcoord + offset, ivec2( 2, 0));

            vec4 texel02 = textureOffset(sampler, texcoord + offset, ivec2(-1, 1));
            vec4 texel12 = textureOffset(sampler, texcoord + offset, ivec2( 0, 1));
            vec4 texel22 = textureOffset(sampler, texcoord + offset, ivec2( 1, 1));
            vec4 texel32 = textureOffset(sampler, texcoord + offset, ivec2( 2, 1));

            vec4 texel03 = textureOffset(sampler, texcoord + offset, ivec2(-1, 2));
            vec4 texel13 = textureOffset(sampler, texcoord + offset, ivec2( 0, 2));
            vec4 texel23 = textureOffset(sampler, texcoord + offset, ivec2( 1, 2));
            vec4 texel33 = textureOffset(sampler, texcoord + offset, ivec2( 2, 2));

            vec2 splineCoord = fract(vec2(textureSize(sampler, 0)) * texcoord);

            vec4 row0 = catmullRom(texel00, texel10, texel20, texel30, splineCoord.x);
            vec4 row1 = catmullRom(texel01, texel11, texel21, texel31, splineCoord.x);
            vec4 row2 = catmullRom(texel02, texel12, texel22, texel32, splineCoord.x);
            vec4 row3 = catmullRom(texel03, texel13, texel23, texel33, splineCoord.x);

            return catmullRom(row0, row1, row2, row3, splineCoord.y);
        }

        void main()
        {
            ivec2 textureSize = textureSize(diffuse, 0);
            color = textureCatmullrom(diffuse, v_st, vec2(offset) / vec2(textureSize));
        }
    </script>

    <script id="fs-bicubic" type="x-shader/x-fragment">
        #version 300 es
        precision highp float;
        precision highp int;

        uniform sampler2D diffuse;

        in vec2 v_st;

        out vec4 color;

        vec4 catmullRom(in vec4 A, in vec4 B, in vec4 C, in vec4 D, in float s)
        {
            mat4 catmullRomMatrix = mat4(
                vec4(-1.0, 2.0,-1.0, 0.0),
                vec4( 3.0,-5.0, 0.0, 2.0),
                vec4(-3.0, 4.0, 1.0, 0.0),
                vec4( 1.0,-1.0, 0.0, 0.0));

            vec4 expo = vec4(s * s * s, s * s, s, 1.0);

            return 0.5 * expo * catmullRomMatrix * mat4(
                A[0], B[0], C[0], D[0],
                A[1], B[1], C[1], D[1],
                A[2], B[2], C[2], D[2],
                A[3], B[3], C[3], D[3]);
        }

        vec4 textureCatmullrom(in sampler2D sampler, in vec2 st)
        {
            ivec2 size = textureSize(sampler, 0);
            ivec2 texelCoord = ivec2(vec2(size) * st);

            vec4 texel00 = texelFetchOffset(sampler, texelCoord, 0, ivec2(-1,-1));
            vec4 texel10 = texelFetchOffset(sampler, texelCoord, 0, ivec2( 0,-1));
            vec4 texel20 = texelFetchOffset(sampler, texelCoord, 0, ivec2( 1,-1));
            vec4 texel30 = texelFetchOffset(sampler, texelCoord, 0, ivec2( 2,-1));

            vec4 texel01 = texelFetchOffset(sampler, texelCoord, 0, ivec2(-1, 0));
            vec4 texel11 = texelFetchOffset(sampler, texelCoord, 0, ivec2( 0, 0));
            vec4 texel21 = texelFetchOffset(sampler, texelCoord, 0, ivec2( 1, 0));
            vec4 texel31 = texelFetchOffset(sampler, texelCoord, 0, ivec2( 2, 0));

            vec4 texel02 = texelFetchOffset(sampler, texelCoord, 0, ivec2(-1, 1));
            vec4 texel12 = texelFetchOffset(sampler, texelCoord, 0, ivec2( 0, 1));
            vec4 texel22 = texelFetchOffset(sampler, texelCoord, 0, ivec2( 1, 1));
            vec4 texel32 = texelFetchOffset(sampler, texelCoord, 0, ivec2( 2, 1));

            vec4 texel03 = texelFetchOffset(sampler, texelCoord, 0, ivec2(-1, 2));
            vec4 texel13 = texelFetchOffset(sampler, texelCoord, 0, ivec2( 0, 2));
            vec4 texel23 = texelFetchOffset(sampler, texelCoord, 0, ivec2( 1, 2));
            vec4 texel33 = texelFetchOffset(sampler, texelCoord, 0, ivec2( 2, 2));

            vec2 splineCoord = fract(vec2(size * texelCoord));

            vec4 row0 = catmullRom(texel00, texel10, texel20, texel30, splineCoord.x);
            vec4 row1 = catmullRom(texel01, texel11, texel21, texel31, splineCoord.x);
            vec4 row2 = catmullRom(texel02, texel12, texel22, texel32, splineCoord.x);
            vec4 row3 = catmullRom(texel03, texel13, texel23, texel33, splineCoord.x);

            return catmullRom(row0, row1, row2, row3, splineCoord.y);
        }

        void main()
        {
            color = textureCatmullrom(diffuse, v_st);
        }
    </script>

    <script type="module" src="./texture_offset.ts">
    </script>
    <div id="highlightedLines"  style="display: none">#L72-L90</div>

</body>

</html>
